//
//  ChoseAddressVC.m
//  SDHuiYuShoping
//
//  Created by luojiao on 2017/6/8.
//  Copyright © 2017年 luojiao. All rights reserved.
//

#import "ChoseAddressVC.h"
#import "ChoseAddressCell.h"
#import "AddressViewController.h"

@interface ChoseAddressVC ()

{
    NSArray *addresArr;
}
@end

@implementation ChoseAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    addresArr = [[NSArray alloc] initWithObjects:@"山东省烟台市芝罘区鲁东国际21楼",@"山东省烟台市芝罘区", nil];
    self.tableView.separatorStyle = NO;

    //设置返回按钮
    [self.backButton setImage:[UIImage imageNamed:@"back_white_btn"] forState:UIControlStateNormal];
    [self.backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];
    [self.backButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)backButton:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)addressButton:(id)sender
{
    AddressViewController *addressVC = [[AddressViewController alloc] init];
    [self.navigationController pushViewController:addressVC animated:YES];
    
}

#pragma mark - tableViewdelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return addresArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChoseAddressCell *cell;
    
    static NSString *cellIdentifier = @"choseCell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ChoseAddressCell" owner:self options:nil][0];
    }
    
    //cell 没有选中状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return   UITableViewCellEditingStyleDelete;
}
//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//进入编辑模式，按下出现的编辑按钮后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    WS(weakself);
    [tableView setEditing:NO animated:YES];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"你确定删除该消息？" preferredStyle:UIAlertControllerStyleActionSheet];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//            //
//            //            [_classArray removeObjectAtIndex:indexPath.row];
//            //            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//            MessageModel *model = weakself.dataArray[indexPath.row];
//            [weakself singleDelet:model.mid];
//            
//        
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
}
//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
//设置进入编辑状态时，Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
